package com.huiqiao;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

public class JDBCDemo {

    public static void main(String[] args) {
        String url = "jdbc:mysql://pm.ahmulang.com:10011/hq";
        String username = "root";
        String password = "8CtEPAEv65FFMQwk";

        String sql = "SELECT * FROM tb_user";

        try {
            // 1. 加载驱动
            Class.forName("com.mysql.cj.jdbc.Driver");

            // 2. 获取连接
            Connection conn = DriverManager.getConnection(url, username, password);

            // 3. 创建 Statement 对象
            PreparedStatement pstmt = conn.prepareStatement(sql);

            // 4. 执行查询
            ResultSet rs = pstmt.executeQuery();

            // 5. 处理结果集
            while (rs.next()) {
                System.out.println(
                        rs.getInt("id") + " | " +
                                rs.getString("username") + " | " +
                                rs.getString("gender") + " | " +
                                rs.getString("email")
                );
            }

            // 6. 关闭资源
            rs.close();
            pstmt.close();
            conn.close();

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
